#include "../ModelEditorConfig.h"
#include "MainMenu.h"
#include "EditorCommandIDs.h"
#include "../MainWindow.h"


StringArray MainMenuComponent::getMenuBarNames()
{
    const char* const names[] = { "File", "Background", "Help", nullptr };

    return StringArray(names);
}

PopupMenu MainMenuComponent::getMenuForIndex(int topLevelMenuIndex, const String& menuName)
{
    PopupMenu menu;


    if(topLevelMenuIndex == 0)
    {
		// "File" menu
        menu.addCommandItem(m_pCommandManager, CommandIDs::open);
        menu.addCommandItem(m_pCommandManager, CommandIDs::close);
		menu.addSeparator();

		PopupMenu ImportModels;
        ImportModels.addCommandItem(m_pCommandManager, CommandIDs::importBase + CommandIDs::eModel_MD2);
        ImportModels.addCommandItem(m_pCommandManager, CommandIDs::importBase + CommandIDs::eModel_MD3);
		menu.addSubMenu ("Import", ImportModels);

		menu.addSeparator();
        menu.addCommandItem(m_pCommandManager, CommandIDs::recentFiles);
        menu.addCommandItem(m_pCommandManager, CommandIDs::clearHistory);

        menu.addSeparator();
        menu.addCommandItem(m_pCommandManager, StandardApplicationCommandIDs::quit);
    }
    else if(topLevelMenuIndex == 1)
    {
		// "Background" menu
        menu.addCommandItem(m_pCommandManager, CommandIDs::setColor);
        menu.addCommandItem(m_pCommandManager, CommandIDs::setPicture);
		menu.addSeparator();
        menu.addCommandItem(m_pCommandManager, CommandIDs::reset);
    }
	else if(topLevelMenuIndex == 2)
	{
		// "Help" menu
		menu.addCommandItem(m_pCommandManager, CommandIDs::showLogs);
        menu.addCommandItem(m_pCommandManager, CommandIDs::showAbout);
		
	}

    return menu;
}



ApplicationCommandTarget* MainMenuComponent::getNextCommandTarget()
{
    // this will return the next parent component that is an ApplicationCommandTarget (in this
    // case, there probably isn't one, but it's best to use this method in your own apps).
    return findFirstTargetParentComponent();
}

void MainMenuComponent::getAllCommands(Array <CommandID>& commands)
{
	commands.add( CommandIDs::importBase + CommandIDs::eModel_MD2 );
	commands.add( CommandIDs::importBase + CommandIDs::eModel_MD3 );

    // this returns the set of all commands that this target can perform..
    const CommandID ids[] = {	CommandIDs::open,
                                CommandIDs::close,
                                CommandIDs::recentFiles,
                                CommandIDs::clearHistory,
                                CommandIDs::setColor,
                                CommandIDs::setPicture,
                                CommandIDs::reset,
                                CommandIDs::showLogs,
                                CommandIDs::showAbout                                  
							};

    commands.addArray(ids, numElementsInArray(ids));
}

// This method is used when something needs to find out the details about one of the commands
// that this object can perform..
void MainMenuComponent::getCommandInfo(CommandID commandID, ApplicationCommandInfo& result)
{
	if( commandID == CommandIDs::importBase+CommandIDs::eModel_MD2 )
	{
	    result.setInfo("Import MD2 Model", "Import the MD2 Model", CommandCategories::files, 0);
		return;
	}
	if( commandID == CommandIDs::importBase+CommandIDs::eModel_MD3 )
	{
		result.setInfo("Import MD3 Model", "Import the MD3 Model", CommandCategories::files, 0);
		return;
	}


    switch(commandID)
    {
    case CommandIDs::open:
        result.setInfo("Open Model", "Open the OyEngine Model", CommandCategories::files, 0);
        result.addDefaultKeypress('o', ModifierKeys::commandModifier);
        break;

    case CommandIDs::close:
        result.setInfo ("Close Model", "Close the loaded Model", CommandCategories::files, 0);
        result.addDefaultKeypress('c', ModifierKeys::commandModifier);
        break;

	case CommandIDs::recentFiles:
        result.setInfo ("Recent Files", "Open the recent loaded Model", CommandCategories::files, 0);
		break;

    case CommandIDs::clearHistory:
        result.setInfo("ClearHistory", "Clear loaded Model history", CommandCategories::files, 0);
		break;

    case CommandIDs::setColor:
        result.setInfo("Set Color", "set background color", CommandCategories::bkground, 0);
        result.addDefaultKeypress('s', ModifierKeys::commandModifier);
		break;

    case CommandIDs::setPicture:
        result.setInfo("Set Picture", "set background picture", CommandCategories::bkground, 0);
        result.addDefaultKeypress('p', ModifierKeys::commandModifier);
 
		break;
    case CommandIDs::reset:
        result.setInfo("Reset", "reset background setting", CommandCategories::bkground, 0);
        result.addDefaultKeypress('r', ModifierKeys::commandModifier);

		break;

    case CommandIDs::showLogs:
        result.setInfo("Show logs", "show editor logs", CommandCategories::help, 0);

		break;

    case CommandIDs::showAbout:
        result.setInfo("About", "show about dialog", CommandCategories::help, 0);

		break;

    default:
        break;
    };
}



// this is the ApplicationCommandTarget method that is used to actually perform one of our commands..
bool MainMenuComponent::perform(const InvocationInfo& info)
{
	if( info.commandID == CommandIDs::importBase+CommandIDs::eModel_MD2 )
	{
	    AlertWindow::AlertIconType icon = AlertWindow::WarningIcon;



        AlertWindow::showMessageBoxAsync(icon,
                                            "This is an AlertWindow",
                                            "And this is the AlertWindow's message. Blah blah blah blah blah blah blah blah blah blah blah blah blah.",
                                            "ok");
		return true;
	}
	if( info.commandID == CommandIDs::importBase+CommandIDs::eModel_MD3 )
	{
        AlertWindow::showOkCancelBox(AlertWindow::QuestionIcon,
                                        "This is an ok/cancel AlertWindow",
                                        "And this is the AlertWindow's message. Blah blah blah blah blah blah blah blah blah blah blah blah blah.",
                                        String::empty,
                                        String::empty,
                                        0,
                                        NULL);
 	
		return true;
	}


    switch(info.commandID)
    {
    case CommandIDs::open:
		{
	    AlertWindow::AlertIconType icon = AlertWindow::WarningIcon;



        AlertWindow::showMessageBoxAsync(icon,
                                            "This is an AlertWindow",
                                            "And this is the AlertWindow's message. Blah blah blah blah blah blah blah blah blah blah blah blah blah.",
                                            "ok");
		}
        break;
    case CommandIDs::close:
		{
			CModelEditorConfig::GetInstance()->CloseScene();
			GetMainFrame()->m_pD3DCanvas->m_ControlPanel->ResetUI();
		}
        break;
	case CommandIDs::recentFiles:
		break;
    case CommandIDs::clearHistory:

        break;

    case CommandIDs::setColor:

        break;

    case CommandIDs::setPicture:

        break;
    case CommandIDs::reset:

        break;

    case CommandIDs::showLogs:

        break;

    case CommandIDs::showAbout:

        break;

    default:
        return false;
    };

    return true;
}

ModelEditorMainFrame* MainMenuComponent::GetMainFrame()
{
	return &(m_pMainAppWindow->mainComp->GetMainFrame());
}

void MainMenuComponent::menuItemSelected(int menuItemID, int topLevelMenuIndex)
{
    // most of our menu items are invoked automatically as commands, but we can handle the
    // other special cases here..

	return;
}